package lion.workflow.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lion.dev.lang.BeanUtil;
import lion.dev.lang.Lang;
import lion.dev.lang.MapJ;
import lion.framework.core.db.DB;
import lion.framework.core.db.DataTable;
import lion.framework.core.db.SessionFactory;
import lion.framework.core.db.paging.SQLPagingFactory;
import lion.workflow.enums.FlowTaskStatus;

import org.apache.commons.lang.StringUtils;

public class FlowTask implements Serializable{

	private static final long serialVersionUID = 1L;
	public static final String TABLE_NAME = "flowtask";

	private static String _SAVESQL="insert into  " + TABLE_NAME +"(processId,stateId,userId,status,createTime,transition,finishTime,note) values (:processId,:stateId,:userId,:status,:createTime,:transition,:finishTime,:note)";
	private static String _UPDATESQL="update " + TABLE_NAME + " set processId=:processId,stateId=:stateId,userId=:userId,status=:status,createTime=:createTime,transition=:transition,finishTime=:finishTime,note=:note where id=:id";
	private static String _DELETESQL="delete from " + TABLE_NAME + " where id=?";
	private static String _DELETEALLSQL="delete from " + TABLE_NAME + " where id in(%s)";
	private static String _GETSQL="select * from " + TABLE_NAME + " where id=?";
	private static String _LISTSQL="select * from " + TABLE_NAME;
	private static String _COUNTSQL="select count(*) from " + TABLE_NAME;


	private int id;
	private int processId;
	private int stateId;
	private int userId;
	private FlowTaskStatus status;
	private Date createTime;
	private String transition;
	private Date finishTime;
	private String note;

	public void setId(int id){this.id=id;}
	public int getId(){return this.id;}
	public void setProcessId(int processId){this.processId=processId;}
	public int getProcessId(){return this.processId;}
	public void setStateId(int stateId){this.stateId=stateId;}
	public int getStateId(){return this.stateId;}
	public void setUserId(int userId){this.userId=userId;}
	public int getUserId(){return this.userId;}
	public void setStatus(FlowTaskStatus status){this.status=status;}
	public FlowTaskStatus getStatus(){return this.status;}
	public void setCreateTime(Date createTime){this.createTime=createTime;}
	public Date getCreateTime(){return this.createTime;}
	public void setTransition(String transition){this.transition=transition;}
	public String getTransition(){return this.transition;}
	public void setFinishTime(Date finishTime){this.finishTime=finishTime;}
	public Date getFinishTime(){return this.finishTime;}
	public void setNote(String note){this.note=note;}
	public String getNote(){return this.note;}


	public void save() {

		DB db = SessionFactory.getCurrentDB();
		this.save(db);
	}

	public void save(DB db) {

		DataTable dt = db.save(_SAVESQL, valueMap());
		if (dt.getRowCount() > 0) {
			MapJ row = dt.getRow(0);
			this.id = row.getInt(row.getKey(0));
		}
	}

	public void update() {

		DB db = SessionFactory.getCurrentDB();
		this.update(db);

	}

	public void update(DB db) {

		db.update(_UPDATESQL, valueMap());
	}

	public void delete() {

		DB db = SessionFactory.getCurrentDB();
		this.delete(db);
	}

	public void delete(DB db) {

		db.update(_DELETESQL, getId());
	}

	public static void delete(DB db, int... ids) {

		if (ids == null || ids.length == 0) { return; }
		String sql = String.format(_DELETEALLSQL, Lang.join(ids));
		db.update(sql);
	}

	public static void delete(int... ids) {

		delete(SessionFactory.getCurrentDB(), ids);
	}

	public static FlowTask load(int id) {

		DB db = SessionFactory.getCurrentDB();
		return load(id, db);
	}

	public static FlowTask load(int id, DB db) {

		MapJ row = db.queryOneRow(_GETSQL, id);

		if(row.isEmpty()){return null;}
		return FlowTask.create(row);

	}
	public static FlowTask load(String where) {

		DB db = SessionFactory.getCurrentDB();
		return load(where, db);
	}

	public static FlowTask load(String where, DB db) {

		return load(where, null, db);
	}

	public static FlowTask load(String where,MapJ param, DB db) {

		String sql = _LISTSQL + (StringUtils.isBlank(where) ? "" : where);

		MapJ row = db.queryOneRow(sql,param);
		if(row.isEmpty()){return null;}

		return FlowTask.create(row);
	}

	public static int count() {

		return count("");
	}

	public static int count(DB db) {

		return count("", db);
	}

	public static int count(String where) {

		return count(where, SessionFactory.getCurrentDB());
	}

	public static int count(String where, MapJ param) {

		return count(where, param, SessionFactory.getCurrentDB());
	}

	public static int count(String where, DB db) {

		return count(where, null, db);
	}

	public static int count(String where, MapJ param, DB db) {

		String sql = _COUNTSQL + (StringUtils.isBlank(where) ? "" : " " + where);
		return db.queryInt(sql, param);

	}
	public static FlowTask create(MapJ map) {

		FlowTask o = new FlowTask();
		try {
			BeanUtil.populate(o, map);
		} catch (Exception e) {
		}

		return o;
	}

	public static List<FlowTask> list(int size) {

		DB db = SessionFactory.getCurrentDB();
		return list(size, db);
	}

	public static List<FlowTask> list(int size, DB db) {

		return list(size, null, db);
	}

	public static List<FlowTask> list(int size, String where) {

		return list(size, where, SessionFactory.getCurrentDB());
	}

	public static List<FlowTask> list(int size, String where, DB db) {

		return list(1, size, where, db);
	}

	public static List<FlowTask> list(int size, String where, MapJ param, DB db) {

		return list(1, size, where, param, db);
	}

	public static List<FlowTask> list(int start, int size) {

		return list(start, size, SessionFactory.getCurrentDB());
	}

	public static List<FlowTask> list(int start, int size, DB db) {

		return list(start, size, null, db);
	}

	public static List<FlowTask> list(int start, int size, String where) {

		return list(start, size, where, SessionFactory.getCurrentDB());
	}

	public static List<FlowTask> list(int start, int size, String where, DB db) {

		return list(start, size, where, null, db);
	}

	public static List<FlowTask> list(int start, int size, String where, MapJ param, DB db) {

		String sql = SQLPagingFactory.paging(db.getType(), _LISTSQL + " " + (StringUtils.isBlank(where) ? "" : where), start, size);

		DataTable dt = null;
		if (param == null) {
			dt = db.query(sql);
		} else {
			dt = db.query(sql, param);
		}

		List<FlowTask> result = new ArrayList<>();

		for (MapJ row : dt.getRows()) {
			result.add(FlowTask.create(row));
		}

		return result;
	}

	public MapJ valueMap() {

		MapJ mapJ = new MapJ();

		mapJ.put("id", this.id);
		mapJ.put("processId", this.processId);
		mapJ.put("stateId", this.stateId);
		mapJ.put("userId", this.userId);
		mapJ.put("status", this.status);
		mapJ.put("createTime", this.createTime);
		mapJ.put("transition", this.transition);
		mapJ.put("finishTime", this.finishTime);
		mapJ.put("note", this.note);

		return mapJ;
	}
}